package zhuzx.solution.ir;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.zhuzx.util.GeneralUtil;
import org.zhuzx.util.JdbcUtil;
import org.zhuzx.util.UnifiedConstants;

public class IrUserInserter {
	
	static final String NORMAL_KEY = "员工已存在，";

	public static void main(String[] args) {
		String name = "伍芳稼";//注意：有insert语句
		String phone = "";
		String msg = insert(name, phone);
		IrDataHelper.showResult(msg);
	}
	
	static String insert(String name, String phone) {
		if (GeneralUtil.isBlank(name)) {
			throw new IllegalArgumentException();
		}
		String sql = "select t1.unno, t1.name, t1.mobile,"
				+ "t2.user_id, t2.qywx_org_name, t2.create_date, t2.clock_in_date\r\n" + 
				"from oa.oa_person t1 left join oa.ir_address_book t2\r\n" + 
				"on t1.unno = t2.user_id\r\n" + 
				"where t1.delete_status = 0 and t1.locked_status = 0\r\n" + 
				"and t1.department_name != '离退职人员'\r\n" +
				"and t1.name = '" + name + "'";
		if (GeneralUtil.isNotBlank(phone)) {
			sql += "\nand t1.mobile = '" + phone + "'";
		}
		Map<String,String> info = IrDataHelper.queryUnique("query user before insert", sql, name);
		if (info == null) {
			return null;
		}
		String userId = info.get("USER_ID");
		if (userId != null) {
			String orgName = info.get("QYWX_ORG_NAME");
			String subRes;
			if (GeneralUtil.isBlank(orgName)) {
				String createTime = info.get("CREATE_DATE");
				String reckonCreateTime = UnifiedConstants.getStandardDaf().format(
						GeneralUtil.getCalculatedDate(new Date(), Calendar.MINUTE, -90));
				if (createTime.compareTo(reckonCreateTime) < 0) {
					throw new RuntimeException("已经过去了90分钟但仍未同步组织信息：\n" + info);
				}
				subRes = "该员工于" + createTime + "录入健康申报系统，"
						+ "尚缺少若干关键信息，需要等待系统批量处理，请1小时后再试，";
			} else {
				subRes = NORMAL_KEY;
			}
			return subRes + "请确认该员工的特征信息：\n" + info;
		}
		userId = info.get("UNNO");
		phone = info.get("MOBILE");
		sql = "select t.id, t.create_date from OA.IR_CLOCK_IN_INFO t\r\n" + 
				"where t.name = '%s' and t.phone_number = '%s'\r\n"
				+ "order by t.create_date desc";
		sql = String.format(sql, name, phone);
		Map<String,String> clockRec = IrDataHelper.queryUnique("查询最近一次打卡",
				JdbcUtil.wrapInLimit(sql, 0, 1, JdbcUtil.getDbConn(null)), name);
		String userDesc = "【" + name + "，" + phone + "，" + userId + "】";
		String clockId = null;
		String clockDate = null;
		if (clockRec != null) {
			clockId = clockRec.get("ID");
			clockDate = clockRec.get("CREATE_DATE").substring(0, 10);
		}
		String[][] values = {
				{"id", userId},
				{"user_id", userId},
				{"name", info.get("NAME")},
				{"phone_number", info.get("MOBILE")},
				{"clock_in_id", clockId},
				{"clock_in_date", clockDate},
				{"CREATE_USER", "zhuzexiang"},
				{"CREATE_DATE", GeneralUtil.getDateTime()},
				{"LOCKED_STATUS", "0"},
				{"DELETE_STATUS", "0"}
		};
		Map<String,String> rec = GeneralUtil.createMapping(values);
		String insSql = JdbcUtil.createInsertSql("oa.ir_address_book", rec);
		JdbcUtil.executeUpdate(insSql);
		return "已录入员工" + userDesc + "的基本信息，还需等待系统处理，"
				+ "因为新录入的员工会初始化为未打卡状态，请1小时后到未打卡名单中查看，"
				+ "确认其个人信息无误后通知员工及时打卡。\n注：不管这位新员工是否已打卡，都需要再打一次。";
	}

}